package com.bysj.dao.impl;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;

import com.bysj.dao.RenCaiDao;
import com.bysj.entity.RenCai;
import com.bysj.util.Dbutils;

public class RenCaiDaoImpl implements RenCaiDao{
	private Dbutils db=new Dbutils();
	@Override
	public void addRenCai(String sql, RenCai rc) {
		Connection conn=db.getConnection();
		PreparedStatement pst=null;
		try {
			pst=conn.prepareStatement(sql);
			pst.setString(1, rc.getTitle());
			pst.setString(2, rc.getType());
			pst.setString(3, rc.getTime());
			pst.setString(4, rc.getContent());
			pst.execute();
		} catch (SQLException e) {
			e.printStackTrace();
		}finally{
			db.closeAll(null, pst, conn);
		}
	}

	@Override
	public void delRenCai(String sql, RenCai rc) {
		Connection conn=db.getConnection();
		PreparedStatement pst=null;
		try {
			pst=conn.prepareStatement(sql);
			pst.setInt(1, rc.getId());
			pst.execute();
		} catch (SQLException e) {
			e.printStackTrace();
		}finally{
			db.closeAll(null, pst, conn);
		}
		
	}

	@Override
	public void updateRenCai(String sql, RenCai rc) {

	}

	@Override
	public int CountAllRenCai(String sql) {
		Connection conn=db.getConnection();
		PreparedStatement pst=null;
		ResultSet set=null;
		int count=0;
		try {
			pst=conn.prepareStatement(sql);
			set=pst.executeQuery();
			if(set.next()){
				count=set.getInt(1);
			}
		} catch (SQLException e) {
			e.printStackTrace();
		}finally{
			db.closeAll(set, pst, conn);
		}
		return count;
	}

	@Override
	public ArrayList<RenCai> FenyeQuery(String sql, int page, int rows) {
		ArrayList<RenCai> list=new ArrayList<RenCai>();
		Connection conn=db.getConnection();
		PreparedStatement pst=null;
		ResultSet  set=null;
		RenCai rc=null;
		try {
			pst=conn.prepareStatement(sql);
			pst.setInt(1, (page-1)*rows);
			pst.setInt(2, rows);
			set=pst.executeQuery();
			while(set.next()){
				rc=new RenCai();
				rc.setId(set.getInt(1));
				rc.setTitle(set.getString(2));
				rc.setType(set.getString(3));
				rc.setTime(set.getString(4));
				rc.setContent(set.getString(5));
				rc.setDocname(set.getString(6));
				list.add(rc);
			}
		} catch (SQLException e) {
			e.printStackTrace();
		}finally{
			db.closeAll(set, pst, conn);
		}
		return list;
	}

	@Override
	public ArrayList<RenCai> queryAllRC(String sql) {
		return null;
	}

	@Override
	public ArrayList<RenCai> queryRCByType(String sql, RenCai renCai) {
		ArrayList<RenCai> list=new ArrayList<RenCai>();
		Connection conn=db.getConnection();
		PreparedStatement pst=null;
		ResultSet  set=null;
		RenCai rc=null;
		try {
			pst=conn.prepareStatement(sql);
			pst.setString(1, renCai.getType());
			set=pst.executeQuery();
			while(set.next()){
				rc=new RenCai();
				rc.setId(set.getInt(1));
				rc.setTitle(set.getString(2));
				rc.setType(set.getString(3));
				rc.setTime(set.getString(4));
				rc.setContent(set.getString(5));
				rc.setDocname(set.getString(6));
				list.add(rc);
			}
		} catch (SQLException e) {
			e.printStackTrace();
		}finally{
			db.closeAll(set, pst, conn);
		}
		return list;
	}

	@Override
	public RenCai queryRenCai(String sql, RenCai rencai) {
		Connection conn=db.getConnection();
		PreparedStatement pst=null;
		ResultSet  set=null;
		RenCai rc=new RenCai();
		try {
			pst=conn.prepareStatement(sql);
			pst.setInt(1, rencai.getId());
			set=pst.executeQuery();
			if(set.next()){
				rc.setId(set.getInt(1));
				rc.setTitle(set.getString(2));
				rc.setType(set.getString(3));
				rc.setTime(set.getString(4));
				rc.setContent(set.getString(5));
				rc.setDocname(set.getString(6));
			}
		} catch (SQLException e) {
			e.printStackTrace();
		}finally{
			db.closeAll(set, pst, conn);
		}
		return rc;
	}

	@Override
	public void updateRCDocbyId(String sql, RenCai rencai) {
		Connection conn=db.getConnection();
		PreparedStatement pst=null;
		try {
			pst=conn.prepareStatement(sql);
			pst.setString(1, rencai.getDocname());
			pst.setInt(2, rencai.getId());
			pst.execute();
		} catch (SQLException e) {
			e.printStackTrace();
		}finally{
			db.closeAll(null, pst, conn);
		}
		
	}

	



}
